package com.ly.service.impl;

import com.ly.mapper.*;
import com.ly.po.CoursesPO;
import com.ly.pojo.Cultivate;
import com.ly.pojo.Selections;
import com.ly.service.CultivateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Transactional
public class CultivateServiceImpl implements CultivateService {

    @Autowired
    private CultivateMapper cultivateMapper;

    @Autowired
    private CoursesMapper coursesMapper;

    @Autowired
    private TStudentClassMapper tStudentClassMapper;

    @Autowired
    private SelectionsMapper selectionsMapper;

    @Autowired
    private StudentsMapper studentsMapper;

    @Override
    public List<CoursesPO> getCoursePOListByClassCode(String classCode) {
        List<String> courseCodeList = cultivateMapper.selectCourseCodeListByClassCode(classCode);
        List<CoursesPO> coursesPOList = new ArrayList<>();
        for (String courseCode : courseCodeList) {
            CoursesPO coursesPO = coursesMapper.selectByCourseCodePO(courseCode);
            coursesPOList.add(coursesPO);
        }
        return coursesPOList;
    }

    @Override
    public List<CoursesPO> getAvailableCourse(String classCode, String semester) {
        List<String> courseCodeList = cultivateMapper.selectCourseCodeListByClassCode(classCode);
        List<CoursesPO> coursesPOList = coursesMapper.selectBySemesterAndNoCourseCodeList(semester, courseCodeList);
        return coursesPOList;
    }

    @Override
    public void saveCourseByClassCode(String classCode, String semester, List<String> courseCodes) {
        for (String courseCode : courseCodes) {
            Cultivate cultivate = new Cultivate();
            cultivate.setClassCode(classCode);
            cultivate.setCourseCode(courseCode);
            cultivate.setSemester(semester);
            cultivate.setGradeStatus("未录入");
            cultivateMapper.insert(cultivate);
        }
    }

    @Override
    public void deleteCourseByClassCode(String classCode, String semester, List<String> courseCodes) {
        for (String courseCode : courseCodes) {
            cultivateMapper.deleteByClassAndCourseCodeAndSemester(classCode, courseCode, semester);
        }
    }

    @Override
    public void selectRequiredCourse(String classCode, List<String> courseCodeList) {
        // 根据班级代码查找学生学号集合
        List<Long> snoList = tStudentClassMapper.selectSnoListByClassCode(classCode);
        // 对每一个学号即学生进行选课
        for (Long sno : snoList) {
            for (String courseCode : courseCodeList) {
                Integer studentId = studentsMapper.selectIdBySno(sno);
                Integer courseId = coursesMapper.selectIdByCourseCode(courseCode);
                // 通过课程代码去获取开设学期
                String semester = coursesMapper.selectSemesterByCourseCode(courseCode);
                Selections selections = new Selections();
                selections.setStudentId(studentId);
                selections.setSelectionTime(new Date());
                selections.setCourseId(courseId);
                selections.setStatus("normal");
                selections.setIsSeckill(0);
                selections.setSemester(semester);
                selectionsMapper.insert(selections);
            }
        }
    }

    @Override
    public void deleteRequiredCourse(String classCode, List<String> courseCodeList) {
        // 根据班级代码查找学生学号集合
        List<Long> snoList = tStudentClassMapper.selectSnoListByClassCode(classCode);
        // 对每一个学号即学生进行退课
        for (Long sno : snoList) {
            for (String courseCode : courseCodeList) {
                Integer studentId = studentsMapper.selectIdBySno(sno);
                Integer courseId = coursesMapper.selectIdByCourseCode(courseCode);
                selectionsMapper.deleteByCourseIdAndStudentId(courseId, studentId);
            }
        }
    }

    @Override
    public List<String> getCourseCodeListByClassCode(String classCode) {
        return cultivateMapper.selectCourseCodeListByClassCode(classCode);
    }
}
